package com.molyfun.parents.activity.coursecenter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.rest.bean.SubjectCommentRequestBody;
import com.willy.ratingbar.BaseRatingBar;
import com.willy.ratingbar.ScaleRatingBar;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CommitCommentActivity extends BaseActivity implements View.OnClickListener, BaseRatingBar.OnRatingChangeListener {

    public static final int REQUEST_SECOND = 1001;

    public static final String SUBJECT_ID = "subject_id";

    @InjectView(R.id.base_title_left)
    ImageView mTitleLeft;
    @InjectView(R.id.commit_comment)
    TextView mCommitComment;
    @InjectView(R.id.simpleRatingBar)
    ScaleRatingBar mScaleRatingBar;
    @InjectView(R.id.base_title)
    TextView mTitle;
    @InjectView(R.id.commit_content)
    EditText mContent;

    private int evaluateNum = 0;

    private String mSubjectId;

    public static void startActivity(Context context, String subjectId){
        Intent intent = new Intent();
        intent.setClass(context, CommitCommentActivity.class);
        intent.putExtra(SUBJECT_ID, subjectId);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commit_comment);
        ButterKnife.inject(this);
        getSupportActionBar().setTitle("");
        mTitle.setText("写评价");
        mTitleLeft.setOnClickListener(this);
        mCommitComment.setOnClickListener(this);
        mScaleRatingBar.setOnRatingChangeListener(this);

        mSubjectId = getIntent().getStringExtra(SUBJECT_ID);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.base_title_left:
                finish();
                break;
            case R.id.commit_comment:
                SubjectCommentRequestBody subjectCommentRequestBody = new SubjectCommentRequestBody();
                subjectCommentRequestBody.setContent(mContent.getText().toString().trim());
                subjectCommentRequestBody.setDegree(evaluateNum);
                addComment(mSubjectId, subjectCommentRequestBody);
                break;
        }
    }

    @Override
    public void onRatingChange(BaseRatingBar baseRatingBar, float v) {
        evaluateNum = (int)v;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private void addComment(String id, SubjectCommentRequestBody subjectCommentRequestBody) {
        showProgressDialog();
        Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().addSubjectComment(id, subjectCommentRequestBody);
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                dismissProgressDialog();
                if (response.body() != null ) {
                    if(response.body().getCode() == 200){
                        toast("评论成功");
                        finish();
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }

}
